/*
 * @Author: chunlai
 * @Date: 2025-11-18 16:47:48
 * @LastEditTime: 2025-11-21 09:29:51
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \01.latest-short-insure\src\store\modules\apis.js
 * @Description: 公共接口
 */
import $http from "@/utils/request";
import { GetToRateData } from "@/translate/risks/rate";
const state = {};
const getters = {};
const mutations = {
  SET_STATE_FIELD(state, payload) {
    Object.assign(state, payload);
  },
};
const actions = {
  comAdminApiRequest({ commit, dispatch }, objData) {
    // console.log("comAdminApiRequest--->", objData.apiName);
    return new Promise((reslove, reject) => {
      commit("updateLoad", true, { root: true });
      $http
        .post(`/ms-insure/api/common/${objData.apiName}`, objData.apiParams, {
          json: true,
        })
        .then(({ data }) => {
          commit("updateLoad", false, { root: true });
          // reslove(data);
          if (data.code == "1") {
            reslove(data);
            if (objData.apiName === "getVarietyInfo") {
              dispatch("varietyInfo/initData", data, { root: true });
            }
            if (objData.apiName === "insertNoticeRead") {
              commit("home/SET_READ_POP", "1", {
                root: true,
              });
            }
          } else {
            if (objData.toast) {
              commit(
                "common/SET_FIELD_MEAN_CONT",
                {
                  label: `温馨提示`,
                  desc: data.msg,
                },
                { root: true }
              );
            }
          }
        })
        .catch((err) => {
          reject("err");
        })
        .finally(() => {
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
  getBillDetails({ commit }, apiParams) {
    // console.log("apis-getBillDetails");
    return new Promise((reslove, reject) => {
      $http
        .post(`/ms-insure/api/common/detail`, apiParams, { json: true })
        .then(({ data }) => {
          reslove(data);
        })
        .catch((err) => {
          reject("err");
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
  getStaffDatas({ commit }, worknum) {
    // console.log("apis-getStaffDatas");
    return new Promise((reslove, reject) => {
      commit("updateLoad", true, { root: true });
      $http
        .post(`/ms-insure/mobile/electricThrow/getStaffInfo`, {
          worknum: worknum,
        })
        .then((res) => {
          reslove(res);
        })
        .catch((err) => {
          reject("err");
        })
        .finally(() => {
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
  userSaveApi({ commit, rootGetters }, apiParams) {
    // console.log("userSaveApi--->");
    return new Promise((reslove, reject) => {
      commit("updateLoad", true, { root: true });
      const yunUserId = rootGetters["insert/yunUserId"],
        conts = rootGetters["insert/cont"],
        appnts = rootGetters["insert/appnt"],
        apiBbrMult = rootGetters["insert/assuredList"],
        msChannel = rootGetters["insert/msChannel"],
        showedRisks = rootGetters["risks/showedRisks"];
      let bbrlist = [];
      const len = apiBbrMult.length;
      for (let i = 0; i < len; i++) {
        const item = apiBbrMult[i];
        const obj = {
          ...JSON.parse(JSON.stringify(item)),
          risks: GetToRateData(showedRisks),
        };
        bbrlist.push(obj);
      }
      let params = {
        cont: conts,
        appnt: appnts,
        insureds: bbrlist,
        msChannel: msChannel,
        ...apiParams,
      };
      if (yunUserId) {
        params.userId = yunUserId;
      }
      $http
        .post(`/ms-insure/api/common/saveCustomerInfo`, params, { json: true })
        .then(({ data }) => {
          commit("updateLoad", false, { root: true });
          reslove(data);
        })
        .catch((err) => {
          reject("err");
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
  riskSaveApi({ commit, rootGetters }, apiParams) {
    // console.log("riskSaveApi--->");
    return new Promise((reslove, reject) => {
      commit("updateLoad", true, { root: true });
      const apiBbrMult = rootGetters["insert/assuredList"],
        totalPrem = rootGetters["insert/totalPrem"],
        conts = rootGetters["insert/cont"],
        showedRisks = rootGetters["risks/showedRisks"];
      let bbrlist = JSON.parse(JSON.stringify(apiBbrMult)),
        len = bbrlist.length;
      for (let i = 0; i < len; i++) {
        const item = bbrlist[i];
        item.risks = GetToRateData(showedRisks);
      }
      let params = {
        ...apiParams,
        cont: {
          ...conts,
          totalPrem: totalPrem,
        },
        insureds: bbrlist,
      };
      $http
        .post(`/ms-insure/api/common/saveRisksInfo`, params, { json: true })
        .then(({ data }) => {
          commit("updateLoad", false, { root: true });
          reslove(data);
        })
        .catch((err) => {
          reject("err");
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
  comAliYunApi({ commit }, objData) {
    console.log("apis-comAliYunApi", objData.apiName);
    return new Promise((reslove, reject) => {
      commit("updateLoad", true, { root: true });
      $http
        .post(`/aliyun/oss/${objData.apiName}`, objData.apiParams)
        .then((res) => {
          reslove(res);
        })
        .finally((err) => {
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
  comConfigApi({ commit, dispatch }, objData) {
    console.log("comConfigApi--->", objData.apiName);
    return new Promise((reslove, reject) => {
      commit("updateLoad", true, { root: true });
      $http
        .post(`/ms-insure/api/common/${objData.apiName}`, objData.apiParams, {
          json: true,
        })
        .then(({ data }) => {
          commit("updateLoad", false, { root: true });
          reslove(data);
        })
        .catch((err) => {
          reject("err");
        })
        .finally(() => {
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
